// -------------------------------------------------------------------------
// PARAMETERS:
// -------------------------------------------------------------------------

//transforms
float4x4 tW: WORLD;        //the models world matrix
float4x4 tV: VIEW;         //view matrix as set via Renderer (EX9)
float4x4 tP: PROJECTION;   //projection matrix as set via Renderer (EX9)
float4x4 tWVP: WORLDVIEWPROJECTION;

//texture1
texture TexA <string uiname="Texture Trigger";>;
sampler SampA = sampler_state    //sampler for doing the texture-lookup
{
    Texture   = (TexA);          //apply a texture to the sampler
    MipFilter = LINEAR;         //sampler states
    MinFilter = LINEAR;
    MagFilter = LINEAR;
};
float4x4 tTexA: TEXTUREMATRIX <string uiname="Texture Transform";>;

//texture2
texture TexB <string uiname="Texture for Manupulating";>;
sampler SampB = sampler_state    //sampler for doing the texture-lookup
{
    Texture   = (TexB);          //apply a texture to the sampler
    MipFilter = LINEAR;         //sampler states
    MinFilter = LINEAR;
    MagFilter = LINEAR;
    AddressU  = WRAP;
    AddressV  = WRAP;
    AddressW  = CLAMP;

};
float4x4 tTexB: TEXTUREMATRIX <string uiname="Texture Transform";>;

//float width <string uiname="width";>  = 256;
//float height <string uiname="height";>  = 256;

//the data structure: "vertexshader to pixelshader"
//used as output data with the VS function
//and as input data with the PS function
struct vs2ps
{
    float4 Pos : POSITION;
    float4 TexCd : TEXCOORD0;
//    float2 Pixeldiameter : TEXCOORD1;
};

// -------------------------------------------------------------------------
// VERTEXSHADERS
// -------------------------------------------------------------------------

vs2ps VS(
    float4 Pos : POSITION,
    float4 TexCd : TEXCOORD0)
{
    //inititalize all fields of output struct with 0
    vs2ps Out = (vs2ps)0;

    //transform position
    Out.Pos = mul(Pos, tWVP);
    
    //transform texturecoordinates
    Out.TexCd = mul(TexCd, tTexA);
    //Out.Pixeldiameter.x = 1/width;
    //Out.Pixeldiameter.y = 1/height;
    return Out;
}

// -------------------------------------------------------------------------
// PIXELSHADERS:
// -------------------------------------------------------------------------

//swap the pixels
float4 SWAP(vs2ps In): COLOR
{
    //get the color of Texture Trigger
    float4 trigger = tex2D(SampA, In.TexCd);
    //use those values (they go from 0 to 1) as y value
    //for the texture lookup of the secound texture
    float4 col = tex3D(SampB, float3(In.TexCd.x, In.TexCd.y, trigger.r));
    return col;
}



float4 BYPASS1(vs2ps In): COLOR
{
    float4 col = tex2D(SampA, In.TexCd);
    return col;
}

float4 BYPASS2(vs2ps In): COLOR
{
    float4 col = tex2D(SampB, In.TexCd);
    return col;
}

// -------------------------------------------------------------------------
// TECHNIQUES:
// -------------------------------------------------------------------------
technique swap
{
    pass P0
    {
        //Wrap0 = U;  // useful when mesh is round like a sphere
        VertexShader = compile vs_2_0 VS();
        PixelShader  = compile ps_2_0 SWAP();
    }
}


technique Bypass_Trigger
{
    pass P0
    {
        //Wrap0 = U;  // useful when mesh is round like a sphere
        VertexShader = compile vs_1_1 VS();
        PixelShader  = compile ps_1_0 BYPASS1();
    }
}

technique Bypass_for_Manupulating
{
    pass P0
    {
        //Wrap0 = U;  // useful when mesh is round like a sphere
        VertexShader = compile vs_1_1 VS();
        PixelShader  = compile ps_1_0 BYPASS2();
    }
}
